package com.bosssoft.boss_exam_db.service.busines.impl;

import com.bosssoft.boss_exam_db.entity.Admin;
import com.bosssoft.boss_exam_db.entity.AdminRole;
import com.bosssoft.boss_exam_db.entity.Company;
import com.bosssoft.boss_exam_db.entity.Role;
import com.bosssoft.boss_exam_db.mapper.AdminMapper;
import com.bosssoft.boss_exam_db.mapper.AdminRoleMapper;
import com.bosssoft.boss_exam_db.mapper.CompanyMapper;
import com.bosssoft.boss_exam_db.mapper.RoleMapper;
import com.bosssoft.boss_exam_db.service.base.impl.BaseServiceImpl;
import com.bosssoft.boss_exam_db.service.busines.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class
AdminServiceImpl extends BaseServiceImpl<Admin> implements AdminService {

    //注入AdminMapper
    @Autowired
    private AdminMapper adminMapper;

    // 注入AdminRoleMapppr
    @Autowired
    private AdminRoleMapper adminRoleMapper;

    //注入角色Mapper
    @Autowired
    private RoleMapper roleMapper;

    //注入公司Mapper
    @Autowired
    private CompanyMapper companyMapper;

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 根据管理员ID查询所拥有的角色
     * @Param[id]
     * @Return java.util.List<com.bosssoft.boss_exam_db.entity.Role>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public List<Role> queryRolesById(String id) {
        Example example = new Example(AdminRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("adminId",id);
        List<AdminRole> adminRoles = adminRoleMapper.selectByExample(example);
        List<Role> roles = adminRoles.stream().map(adminRole -> {
            String roleid = adminRole.getRoleId();
            Role role = roleMapper.selectByPrimaryKey(roleid);
            return role;
        }).collect(Collectors.toList());
        return roles;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 分页查询管理员
     * @Param [page, limit, sort, order]
     * @Return com.github.pagehelper.PageInfo<com.bosssoft.boss_exam_db.entity.Admin>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public PageInfo<Admin> queryByCondition(Integer page,Integer limit, String sort, String order) {
        String orderStr=sort+" "+order;
        Example example = new Example(Admin.class);
        final PageInfo<Admin> pageInfo = PageHelper.startPage(page, limit).setOrderBy(orderStr).doSelectPageInfo(() ->
            this.adminMapper.selectByExample(example)
        );
        return pageInfo;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/16
     * @Description 根据管理员ID查询管理员所在公司的详情
     * @Param [adminId] 管理员ID
     * @Return com.bosssoft.boss_exam_db.entity.Company
     * @Exception //无
     * @since JDK 1.8
     **/
    @Override
    public Company selectAdminBelongCompany(String adminId) {
        Company company=null;
        Admin admin = this.adminMapper.selectByPrimaryKey(adminId);
        if(admin!=null&&admin.getDeleted()==false){
            String companyId = admin.getCompanyId();
            if(companyId!=null){
                company = this.companyMapper.selectByPrimaryKey(companyId);
            }
        }
        if(company.getDeleted()){
            return null;
        }else {
            return company;
        }
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/25
     * @Description 根据部门ID查询该部门的所有管理员
     *
     * @Param [id] 部门id
     * @Return java.util.List<com.bosssoft.boss_exam_db.entity.Admin>
     * @Exception 无
     * @since JDK 1.8
     **/
    @Override
    public List<Admin> queryAdminByDepartment(String id) {
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(id)){
            criteria.andEqualTo("departmentId",id);
        }
        criteria.andEqualTo("deleted",false);
        List<Admin> admins = adminMapper.selectByExample(example);
        return admins;
    }
}
